package com.leyou.user.service.controller;

import com.leyou.common.bean.Result;
import com.leyou.user.inter.dto.UserAdressUpdateDTO;
import com.leyou.user.inter.pojo.Area;
import com.leyou.user.inter.pojo.UserAdress;
import com.leyou.user.service.service.AdressService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zqq
 * @ClassName AdressController
 * @Description
 * @date 2020/4/5-14:14
 */
@RestController
@Api(value = "AdressController",description = "地址接口")
@RequestMapping("adress")
public class AdressController {

    @Autowired
    private AdressService adressService;

    @GetMapping("query/area/{parentId}")
    @ApiOperation(value = "行政区域查询，查询省级parentId传0", notes = "行政区域查询")
    public Result<List<Area>> queryAreaByParentId(
            @ApiParam(name = "parentId",value = "上级id",required = true) @PathVariable(value = "parentId") Integer parentId) {
        return this.adressService.queryAreaByParentId(parentId);
    }

    @GetMapping("query/adress/all")
    @ApiOperation(value = "查询用户所有收货地址", notes = "查询用户所有收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result<List<UserAdress>> queryUserAdressByAll() {
        return this.adressService.queryUserAdressByAll();
    }

    @GetMapping("query/adress/by/{id}")
    @ApiOperation(value = "根据主键查询收货地址", notes = "根据主键查询收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result<UserAdress> queryUserAdressById(@ApiParam(name = "id",value = "收货地址id",required = true) @PathVariable(value = "id") Long id) {
        return this.adressService.queryUserAdressById(id);
    }

    @GetMapping("query/default/adress")
    @ApiOperation(value = "查询用户默认收货地址", notes = "查询用户默认收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result<UserAdress> queryUserAdressByDefault() {
        return this.adressService.queryUserAdressByDefault();
    }

    @PostMapping("add")
    @ApiOperation(value = "添加收货地址", notes = "添加收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result addUserAdress(@RequestBody UserAdressUpdateDTO dto) {
        return this.adressService.addUserAdress(dto);
    }

    @PutMapping("update")
    @ApiOperation(value = "修改收货地址", notes = "修改收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result updateUserAdress(@RequestBody UserAdressUpdateDTO dto) {
        return this.adressService.updateUserAdress(dto);
    }

    @DeleteMapping("delete/{id}")
    @ApiOperation(value = "删除收货地址", notes = "删除收货地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result deleteUserAdress(@ApiParam(name = "id",value = "收货地址id",required = true) @PathVariable(value = "id") Long id) {
        return this.adressService.deleteUserAdress(id);
    }

    @PutMapping("update/defaul/{id}")
    @ApiOperation(value = "设置默认地址", notes = "设置默认地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "header", name = "x-authorize-token", dataType = "string", required = true, value = "登陆token")})
    public Result updateDefaultAdress(@ApiParam(name = "id",value = "收货地址id",required = true) @PathVariable(value = "id") Long id) {
        return this.adressService.updateDefaultAdress(id);
    }

}
